/**
 * 
 */
package test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

/**
 * @author dato
 */
public class GetUdbUserByID {
	private static HttpClient loginClient = new DefaultHttpClient();

	public void outputUidsByLongIds(List<String[]> params, String file) {
		String authorization = null;
		try {
			byte[] token = "readonly:udb102".getBytes("utf-8");
			authorization = "Basic "
					+ new String(Base64.encodeBase64(token), "utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		String url = "http://udb.vip.xyi.cn.alidc.net/person/get_person_by_uid.htm";
		Map<String, String> headers = new HashMap<String, String>();
		headers.put("Authorization", authorization);
		Writer out;
		try {
			out = new FileWriter(file);
			new BufferedWriter(out);
			out.append("uid,loginId,mobile\r\n");
			for (String[] param : params) {
				List<NameValuePair> formparams = new ArrayList<NameValuePair>();
				formparams.add(new BasicNameValuePair("uid", param[0]));
				String result = doPost(url, formparams, headers);
				Map map = JacksonHelper.fromJSON(result, Map.class);
				String a = param[0] +","+ map.get("loginId")+","+ map.get("mobile")+"\r\n";
				System.out.print(a);
				out.append(a);
			}
			out.flush();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	public String doPost(String url, List<NameValuePair> formparams,
			Map<String, String> headers) {
		HttpPost post = new HttpPost(url);
		StringBuffer sb = new StringBuffer();
		try {
			if (headers != null) {
				for (Entry<String, String> header : headers.entrySet()) {
					post.addHeader(header.getKey(), header.getValue());
				}
			}
			post.setEntity(new UrlEncodedFormEntity(formparams, "UTF-8"));
			HttpResponse response = loginClient.execute(post);
			InputStream input = response.getEntity().getContent();
			BufferedReader in = new BufferedReader(new InputStreamReader(input));
			String inputLine = null;
			while ((inputLine = in.readLine()) != null) {
				sb.append(inputLine);
			}
			;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sb.toString();
	}

	public String doPost(String url, List<NameValuePair> formparams) {
		return doPost(url, formparams, null);
	}

	public String doRequest(String url) {
		HttpUriRequest request = new HttpGet(url);
		HttpResponse response = null;
		StringBuffer sb = new StringBuffer();
		try {
			response = loginClient.execute(request);
			InputStream input = response.getEntity().getContent();
			BufferedReader in = new BufferedReader(new InputStreamReader(input));
			String inputLine = null;
			while ((inputLine = in.readLine()) != null) {
				sb.append(inputLine);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sb.toString();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		GetUdbUserByID rc = new GetUdbUserByID();
		// preUdbData();
		rc.outputUidsByLongIds(preUdbData(), "D://资料//阿里物流//udb信息查询//msg.csv");

	}

	public static List<String[]> preUdbData() {
		List<String[]> ls = new ArrayList<String[]>();

		FileReader reader = null;
		BufferedReader br = null;
		try {
			reader = new FileReader("D://资料//阿里物流//udb信息查询//uid.csv");
			br = new BufferedReader(reader);
			String s1 = null;
			while ((s1 = br.readLine()) != null) {
				System.out.println("开始加载数据：  " + s1);
				String[] aa = s1.split(",");
				ls.add(aa);
			}
		} catch (Exception ex) {
			System.out
					.println("#####################错误##############################");
		} finally {
			try {
				br.close();
				reader.close();
			} catch (Exception e) {
				System.out
						.println("#####################错误$$$$$$$$$$$$$$$$$$$$$$$##############################");
			}
		}

		return ls;
	}
}
